package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.CompanyAndUserV2Param;
import com.dmap.admin.controller.backgroundv2.param.CompanySelectParam;
import com.dmap.admin.dao.CompanyMapper;
import com.dmap.admin.po.Company;
import com.dmap.admin.po.CompanyExample;
import com.dmap.admin.service.backgroundv2.CompanyService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 02/05/18
 * \* Description:公司相关接口实现类
 * \
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public Company insertCompany(CompanyAndUserV2Param param) {
        Company company = new Company();
        company.setNumber(param.getNumber());
        company.setName(param.getName());
        company.setCompanyPhone(param.getCompanyPhone());
        if (StringUtils.isNotBlank(param.getOwnedByCompanyId()) && StringUtils.isNumeric(param.getOwnedByCompanyId())) {
            company.setOwnedByCompanyId(Long.valueOf(param.getOwnedByCompanyId()));
        }
        if (StringUtils.isNotBlank(param.getAdministratorId()) && StringUtils.isNumeric(param.getAdministratorId())) {
            company.setAdministratorId(Long.valueOf(param.getAdministratorId()));
        }
        company.setAdministratorAccount(param.getAdministratorAccount());
        company.setAdministratorHandphone(param.getAdministratorHandphone());
        company.setProvince(param.getProvince());
        company.setCity(param.getCity());
        company.setLogoUrl(param.getLogoUrl());
        company.setServicePhone(param.getServicePhone());
        company.setServiceEmail(param.getServiceEmail());
        company.setDescription(param.getDescription());
        company.setStatus(AdminConstant.NORMAL_STATUS);
        company.setCompanyType(param.getCompanyType());
        if (StringUtils.isNotBlank(param.getCreatorId()) && StringUtils.isNumeric(param.getCreatorId())) {
            company.setCreatorId(Long.valueOf(param.getCreatorId()));
            company.setEditorId(company.getCreatorId());
        }
        company.setCreateTime(System.currentTimeMillis() / 1000);
        company.setUpdateTime(company.getCreateTime());
        int success = companyMapper.insertSelective(company);
        return (success == 1) ? company : null;
    }

    @Override

    public boolean updateCompany(CompanyAndUserV2Param param) {
        Company company = new Company();
        company.setId(Long.valueOf(param.getId()));
        company.setNumber(param.getNumber());
        company.setName(param.getName());
        company.setCompanyPhone(param.getCompanyPhone());
        if (StringUtils.isNotBlank(param.getOwnedByCompanyId()) && StringUtils.isNumeric(param.getOwnedByCompanyId())) {
            company.setOwnedByCompanyId(Long.valueOf(param.getOwnedByCompanyId()));
        }
        if (StringUtils.isNotBlank(param.getAdministratorId()) && StringUtils.isNumeric(param.getAdministratorId())) {
            company.setAdministratorId(Long.valueOf(param.getAdministratorId()));
        }
        company.setAdministratorAccount(param.getAdministratorAccount());
        company.setAdministratorHandphone(param.getAdministratorHandphone());
        company.setProvince(param.getProvince());
        company.setCity(param.getCity());
        company.setLogoUrl(param.getLogoUrl());
        company.setServicePhone(param.getServicePhone());
        company.setServiceEmail(param.getServiceEmail());
        company.setDescription(param.getDescription());
        company.setStatus(param.getStatus());
        company.setCompanyType(param.getCompanyType());
        company.setUpdateTime(System.currentTimeMillis() / 1000);
        return companyMapper.updateByPrimaryKeySelective(company) == 1;
    }

    @Override
    public Company selectById(Long id) {
        return companyMapper.selectByPrimaryKey(id);
    }

    @Override
    public Company selectByAdministratorId(Long administratorId) {
        CompanyExample example = new CompanyExample();
        example.createCriteria().andAdministratorIdEqualTo(administratorId);
        List<Company> list = companyMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<Company> selectByIdList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        CompanyExample example = new CompanyExample();
        example.createCriteria().andIdIn(idList);
        example.setLimitStart(0);
        example.setLimitEnd(idList.size());
        return companyMapper.selectByExample(example);
    }

    @Override
    public List<Company> selectChildrenById(Long id) {
        CompanyExample example = new CompanyExample();
        example.createCriteria().andOwnedByCompanyIdEqualTo(id);
        int pageSize = companyMapper.countByExample(example);
        if (pageSize < 1) {
            return Collections.emptyList();
        }
        example.setLimitStart(0);
        example.setLimitEnd(pageSize);
        return companyMapper.selectByExample(example);
    }

    @Override
    public boolean validateNumberOrNameOrAdministratorIdIsExist(String number, String name, String administratorId) {
        boolean returnValue = true;
        CompanyExample example = new CompanyExample();
        //匹配 公司名称 进行查询
        CompanyExample.Criteria criteria1 = example.createCriteria();
        //匹配 公司管理员ID 进行查询
        CompanyExample.Criteria criteria2 = example.createCriteria();
        //匹配 公司编号 进行查询
        CompanyExample.Criteria criteria3 = example.createCriteria();
        if (StringUtils.isNotBlank(name)) {
            criteria1.andNameEqualTo(name);
        }
        if (StringUtils.isNotBlank(administratorId) && StringUtils.isNumeric(administratorId)) {
            criteria2.andAdministratorIdEqualTo(Long.valueOf(administratorId));
        }
        if (StringUtils.isNotBlank(number)) {
            criteria3.andNumberEqualTo(number);
        }
        example.or(criteria2);
        example.or(criteria3);
        List<Company> list = companyMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            returnValue = false;
        }
        return returnValue;
    }

    @Override
    public int countByCompanySelectParam(CompanySelectParam param) {
        CompanyExample example = this.getCompanySelectParamExample(param.getSearchKeyword());
        return companyMapper.countByExample(example);
    }

    @Override
    public List<Company> selectByCompanySelectParam(CompanySelectParam param) {
        CompanyExample example = this.getCompanySelectParamExample(param.getSearchKeyword());
        //根据前端所传参数不同生成对应的orderBy子句
        String orderByClause = AdminConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), AdminConstant.CREATE_TIME)
                + AdminConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), AdminConstant.DESC);
        example.setOrderByClause(orderByClause);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return companyMapper.selectByExample(example);
    }

    private CompanyExample getCompanySelectParamExample(String searchKeyword) {
        CompanyExample example = new CompanyExample();
        //根据 名称 模糊搜索
        CompanyExample.Criteria criteria1 = example.createCriteria();
        //根据 编号 模糊搜索
        CompanyExample.Criteria criteria2 = example.createCriteria();
        //根据 管理员ID 模糊搜索
        CompanyExample.Criteria criteria3 = example.createCriteria();
        //根据 公司联系电话 模糊搜索
        CompanyExample.Criteria criteria4 = example.createCriteria();
        criteria1.andCompanyTypeEqualTo(AdminConstant.COMPANY_TYPE_BRANCH);
        criteria2.andCompanyTypeEqualTo(AdminConstant.COMPANY_TYPE_BRANCH);
        criteria3.andCompanyTypeEqualTo(AdminConstant.COMPANY_TYPE_BRANCH);
        criteria4.andCompanyTypeEqualTo(AdminConstant.COMPANY_TYPE_BRANCH);
        if (StringUtils.isNotBlank(searchKeyword)) {
            criteria1.andNameLike("%" + searchKeyword + "%");
            criteria2.andNumberLike("%" + searchKeyword + "%");
            criteria3.andAdministratorAccountLike("%" + searchKeyword + "%");
            criteria4.andCompanyPhoneLike("%" + searchKeyword + "%");
        }
        example.or(criteria2);
        example.or(criteria3);
        example.or(criteria4);
        return example;
    }

}